<template>
  <a-table
    :columns="columns"
    :dataSource="data"
    :pagination="pagination"
    :rowSelection="rowSelection"
    @change="handleTableChange"
  >
    <template #name="{ text }">
      <a>{{ text }}</a>
    </template>
    <template #action="{ record }">
      <a-space>
        <a-button type="link" @click="handleReply(record)">回复</a-button>
        <a-button type="link" @click="handleDelete(record)">删除</a-button>
        <a-button type="link">查看</a-button>
      </a-space>
    </template>
  </a-table>
</template>

<script setup>
import { ref, reactive, defineProps, defineEmits } from "vue";
import { CheckSquareTwoTone, DeleteTwoTone } from "@ant-design/icons-vue";

// Props
const props = defineProps({
  data: Array,
});

// Emits
const emit = defineEmits(["update:data"]);

// Reactive state
const pagination = reactive({
  current: 1,
  pageSize: 10,
  total: 0,
});

const rowSelection = reactive({
  selectedRowKeys: [],
  onChange: (selectedRowKeys) => {
    rowSelection.selectedRowKeys = selectedRowKeys;
  },
});

const columns = ref([
  {
    title: "用户名称",
    dataIndex: "name",
    key: "name",
    scopedSlots: { customRender: "name" },
  },
  {
    title: "点评时间",
    dataIndex: "time",
    key: "time",
  },
  {
    title: "游戏评分",
    dataIndex: "score",
    key: "score",
  },
  {
    title: "评论内容",
    dataIndex: "content",
    key: "content",
  },
  {
    title: "设备型号",
    dataIndex: "model",
    key: "model",
  },
  {
    title: "回复数量",
    dataIndex: "replies",
    key: "replies",
  },
  {
    title: "点赞数量",
    dataIndex: "likes",
    key: "likes",
  },
  {
    title: "操作",
    key: "action",
    scopedSlots: { customRender: "action" },
  },
]);

function handleTableChange(pagination, filters, sorter) {
  const params = {
    currentPage: pagination.current,
    pageSize: pagination.pageSize,
    ...filters,
    ...sorter,
  };
  // 调用父组件的方法来更新数据
  emit("update:data", params);
}

function handleReply(record) {
  // 处理回复逻辑
  console.log("Reply to:", record);
}

function handleDelete(record) {
  // 处理删除逻辑
  console.log("Delete:", record);
  // 从选中项中移除
  const index = rowSelection.selectedRowKeys.indexOf(record.key);
  if (index !== -1) {
    rowSelection.selectedRowKeys.splice(index, 1);
  }
  // 更新数据
  emit(
    "update:data",
    props.data.filter((item) => item.key !== record.key)
  );
}

function handleBatchDelete() {
  // 处理批量删除逻辑
  if (rowSelection.selectedRowKeys.length > 0) {
    const keys = rowSelection.selectedRowKeys;
    // 更新数据
    emit(
      "update:data",
      props.data.filter((item) => !keys.includes(item.key))
    );
    // 清空选中项
    rowSelection.selectedRowKeys = [];
  }
}
</script>

<style scoped>
/* 你的样式 */
</style>
